/**
 * Created by jocky on 06/09/2017.
 */
var userObj = {


    /**
     * 实名验证
     */
    realNameAuth: function () {
        swal({
            title: '确定提交实名资料吗？',
            text: "提交后需要等待后台的审核，请认真填写您的实名信息",
            type: 'warning',
            showCancelButton: true,
            cancelButtonColor: '#d33',
            cancelButtonText: '取消',
            confirmButtonColor: '#3085d6',
            confirmButtonText: '确定'
        }).then(function () {
            userObj.realNameAuthSubmit();
        }).catch(swal.noop);
    },
    realNameAuthSubmit: function () {
        var form = $("#form_realNameAuth");
        var params = {};
        var validFlag = true;
        $.each(form.find("input,select"), function (i, v) {
            if (!v.name) return true;
            var required = $(v).attr('data-required');

            var val;
            var tagName = $(v).prop('tagName');
            if (tagName == 'SELECT' || tagName == 'select') {
                val = parseInt(v.value);
                if (required == 1 && val == 0) {
                    setNotify($(v).attr('data-error'), true);

                    validFlag = false;
                    return false;
                }
                params[v.name] = val;
            } else {
                var type = $(v).attr('type');
                switch (type) {
                    case 'FILE':
                    case 'file':
                        var obj = form.find("input[name=" + v.name + "]");
                        var objImg = obj.parent().parent().parent().parent().find('img');
                        var base64Img = objImg.attr('src');
                        required = $(obj).attr('data-required');
                        if (required == 1 && !base64Img) {
                            setNotify($(v).attr('data-error'), true);

                            validFlag = false;
                            return false;
                        }

                        var img = new Image();
                        img.src = base64Img;

                        var width = img.width;
                        var height = img.height;
                        var maxWidthSize = 800;
                        var scale = 1;
                        if (width > maxWidthSize || height > maxWidthSize) {
                            if (width > height) {
                                scale = maxWidthSize / width;
                            } else {
                                scale = maxWidthSize / height;
                            }
                        }

                        if (scale == 1) {
                            params[v.name] = base64Img;
                        } else {
                            var canvas = document.createElement("canvas");
                            var ctx = canvas.getContext("2d");

                            canvas.width = scale * width;
                            canvas.height = scale * height;
                            ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
                            var nData = canvas.toDataURL('image/jpeg');
                            params[v.name] = nData;
                        }
                        break;
                    case 'RADIO':
                    case 'radio':
                        if ($(v).is(':checked')) {
                            params[v.name] = parseInt(v.value);
                        }
                        break;
                    default:
                        val = $.trim(v.value);
                        if (required == 1 && !val) {
                            $(v).focus();
                            setNotify($(v).attr('data-error'), true);

                            validFlag = false;
                            return false;
                        }
                        params[v.name] = val;
                        break;
                }
            }
        });

        if (!validFlag) {
            return false;
        }
        $.post("/user/info-real-name-submit", params, function (json) {
            if (json.code == 1) {
                var html = "<i class='fa fa-check-circle-o' style='color: green'></i> " +
                    "<a href='javascript:void(0)' onclick=\"$('#menu_2_2').trigger('click')\">已实名</a>";
                $(".profile-data-title").html(html);

                $('#modal_pop').modal('hide');
            }

            var obj = {
                title: '提示',
                text: json.msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: json.code == 1 ? 'info' : 'error'
            };
            new PNotify(obj);

            if (json.code == 1) {
                menu_detail['menu_2_2'].loadContent = true;
                get_content('menu_2_2');
            }
        }, 'json');
    }
};